let isDragging = false
let initPosition = {
  x: 0,
  y: 0
}
export const renderDrag = (elName) => {
  document.getElementById(elName).addEventListener('mousedown', (e) => {
    isDragging = true
    initPosition = { x: e.screenX, y: e.screenY }
    e.preventDefault()
  })
  document.getElementById(elName).addEventListener('mousemove', (e) => {
    // e.buttons == 1 鼠标左键按下
    if (isDragging && e.buttons == 1) {
      const offsetX = e.screenX - initPosition.x
      const offsetY = e.screenY - initPosition.y
      window.api.apiDragRender({ offsetX, offsetY })
      initPosition = { x: e.screenX, y: e.screenY }
    }
    e.preventDefault()
  })
  document.getElementById(elName).addEventListener('mouseup', (e) => {
    isDragging = false
  })
}
export default renderDrag
